package org.uoss.core.util;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

public final class AnnotationUtil {

	/**
	 * 
	 * @param annotationedClass
	 * @param annotationClass
	 * @return
	 */
	public static Method getMethodOfAnnotationedClass(
			Class<?> annotationedClass,
			Class<? extends Annotation> annotationClass) {
		if (annotationedClass == null)
			return null;
		for (Method m : annotationedClass.getDeclaredMethods()) {
			if (annotationClass != null) {
				if (m.isAnnotationPresent(annotationClass)) {
					if (!m.isAccessible())
						m.setAccessible(true);
					return m;
				}
			} else if (m.getAnnotations() != null) {
				if (!m.isAccessible())
					m.setAccessible(true);
				return m;
			}
		}
		return getMethodOfAnnotationedClass(annotationedClass.getSuperclass(),
				annotationClass);
	}

	/**
	 * 
	 * @param annotationedClass
	 * @param annotationClass
	 * @return
	 */
	public static Field getFieldOfAnnotationedClass(Class<?> annotationedClass,
			Class<? extends Annotation> annotationClass) {
		if (annotationedClass == null)
			return null;
		for (Field f : annotationedClass.getDeclaredFields()) {
			if (annotationClass != null) {
				if (f.isAnnotationPresent(annotationClass)) {
					if (!f.isAccessible())
						f.setAccessible(true);
					return f;
				}
			} else if (f.getAnnotations() != null) {
				if (!f.isAccessible())
					f.setAccessible(true);
				return f;
			}
		}
		return getFieldOfAnnotationedClass(annotationedClass.getSuperclass(),
				annotationClass);
	}
}
